local jiaoying = fk.CreateSkill{
  name = "mod__jiaoying",
}
local jiaoying_ = {
  events = {fk.AfterCardsMove},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      for _, move in ipairs(data) do
        if move.from == player.id and move.to and move.to ~= player.id and move.toArea == Card.PlayerHand then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local mod__jiaoying_targets = type(player:getMark("mod__jiaoying_targets-turn")) == "table" and player:getMark("mod__jiaoying_targets-turn") or {}
    for _, move in ipairs(data) do
      if move.from == player.id and move.to and move.to ~= player.id and move.toArea == Card.PlayerHand then
        local to = room:getPlayerById(move.to)
        local mod__jiaoying_colors = type(to:getMark("mod__jiaoying_colors-turn")) == "table" and to:getMark("mod__jiaoying_colors-turn") or {}
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand then
            local color = Fk:getCardById(info.cardId).color
            if color ~= Card.NoColor then
              table.insertIfNeed(mod__jiaoying_colors, color)
              table.insertIfNeed(mod__jiaoying_targets, to.id)
              if to:getMark("@mod__jiaoying-turn") == 0 then
                room:setPlayerMark(to, "@mod__jiaoying-turn", {})
              end
            end
          end
        end
        room:setPlayerMark(to, "mod__jiaoying_colors-turn", mod__jiaoying_colors)
      end
    end
    room:setPlayerMark(player, "mod__jiaoying_targets-turn", mod__jiaoying_targets)
  end,
}
local jiaooying_refresh = {
  refresh_events = {fk.PreCardUse},
  can_refresh = function(self, event, target, player, data)
    local mod__jiaoying_targets = type(player:getMark("mod__jiaoying_targets-turn")) == "table" and player:getMark("mod__jiaoying_targets-turn") or {}
    local mod__jiaoying_ignores = type(player:getMark("mod__jiaoying_ignores-turn")) == "table" and player:getMark("mod__jiaoying_ignores-turn") or {}
    return table.contains(mod__jiaoying_targets, target.id) and not table.contains(mod__jiaoying_ignores, target.id)
  end,
  on_refresh = function(self, event, target, player, data)
    local mod__jiaoying_ignores = type(player:getMark("mod__jiaoying_ignores-turn")) == "table" and player:getMark("mod__jiaoying_ignores-turn") or {}
    table.insert(mod__jiaoying_ignores, target.id)
    player.room:setPlayerMark(player, "mod__jiaoying_ignores-turn", mod__jiaoying_ignores)
    player.room:setPlayerMark(target, "@mod__jiaoying-turn", {"mod__jiaoying_usedcard"})
  end,
}
local jiaoying_delay = {
  events = {fk.EventPhaseStart},
  frequency = Skill.Compulsory,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target.phase == Player.Finish then
      local mod__jiaoying_targets = type(player:getMark("mod__jiaoying_targets-turn")) == "table" and player:getMark("mod__jiaoying_targets-turn") or {}
      local mod__jiaoying_ignores = type(player:getMark("mod__jiaoying_ignores-turn")) == "table" and player:getMark("mod__jiaoying_ignores-turn") or {}
      self.cost_data = #mod__jiaoying_targets - #mod__jiaoying_ignores
      if self.cost_data > 0 then
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x = self.cost_data
    local targets = player.room:askForChoosePlayers(player, table.map(table.filter(room.alive_players, function (p)
      return p:getHandcardNum() < 5 end), Util.IdMapper), 1, x, "#mod__jiaoying-choose:::" .. x, self.name, true)
    if #targets > 0 then
      room:sortPlayersByAction(targets)
      for _, pid in ipairs(targets) do
        local to = room:getPlayerById(pid)
        if not to.dead and to:getHandcardNum() < to.maxHp then
          if to.maxHp-to:getHandcardNum() > 5 then
            to:drawCards(5, self.name)
          else
            to:drawCards(to.maxHp-to:getHandcardNum(), self.name)
          end
        end
      end
    end
  end,
}
local jiaoying_prohibit = {
  prohibit_use = function(self, player, card)
    local mod__jiaoying_colors = player:getMark("mod__jiaoying_colors-turn")
    return type(mod__jiaoying_colors) == "table" and table.contains(mod__jiaoying_colors, card.color)
  end,
  prohibit_response = function(self, player, card)
    local mod__jiaoying_colors = player:getMark("mod__jiaoying_colors-turn")
    return type(mod__jiaoying_colors) == "table" and table.contains(mod__jiaoying_colors, card.color)
  end,
}
jiaoying:addEffect(fk.AfterCardsMove, jiaoying_)
jiaoying:addEffect(fk.PreCardUse, jiaooying_refresh)
jiaoying:addEffect(fk.EventPhaseStart, jiaoying_delay)
jiaoying:addEffect("prohibit", jiaoying_prohibit)
return jiaoying